/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.krArmor;

import java.util.ArrayList;
import java.util.Collection;
import org.hamcrest.Matcher;
import org.hamcrest.Matchers;
import ru.jSky.interfaces.Armor;
import ru.jSky.interfaces.Keyword;
import ru.jSky.interfaces.Major;
import ru.jSky.matcher.AlwaysTrueMatcher;
import ru.jSky.matcher.builder.major.ArmorMatcherBuilder;
import ru.jSky.matcher.builder.major.KeywordMatcherBuilder;

/**
 *
 * @author Rakev
 */
public class KRArmorMatcherBuilder {

    private Collection<String> keywordEdidRegexs = new ArrayList<>();
    private String enchantmentEdidRegex;

    public KRArmorMatcherBuilder setEnchantmentRestriction(String edidRegex) {
        enchantmentEdidRegex = edidRegex;
        return this;
    }

    public KRArmorMatcherBuilder addKeywordRestriction(String edidRegex) {
        keywordEdidRegexs.add(edidRegex);
        return this;
    }

    public Matcher<Armor> build() {
        Matcher<Armor> resultMatcher = new AlwaysTrueMatcher();
        if (!keywordEdidRegexs.isEmpty()) {
            resultMatcher = Matchers.allOf(resultMatcher, createKeywordsMatcher());
        }
        if (enchantmentEdidRegex != null) {
            resultMatcher = Matchers.allOf(resultMatcher, createEnchantmentMatcher());
        }
        return resultMatcher;
    }

    private Matcher<Armor> createKeywordsMatcher() {
        Collection<Matcher<? super Armor>> keywordsMatchers = new ArrayList<>(keywordEdidRegexs.size());
        for (String edidRegex : keywordEdidRegexs) {
            Matcher<Keyword> edidMatcher = KeywordMatcherBuilder.create().edid().matches(edidRegex);
            ArmorMatcherBuilder.create().keywords().anyOf(edidMatcher);
        }
        return Matchers.allOf(keywordsMatchers);
    }

    private Matcher<Armor> createEnchantmentMatcher() {
        return ArmorMatcherBuilder.create().enchantment().edid().matches(enchantmentEdidRegex);
    }
}
